package com.midian.action.mc.common;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.midian.common.utils.EnumGetFileContentType;
import com.midian.common.utils.ExcelHelper;
import com.midian.common.utils.FUtil;
import com.midian.common.utils.FileUploadUtil;

/**
 * 文件处理Action
 * @author Mengfh
 *
 */
@Scope("prototype")
@Controller("fileOpAction")
public class FileOpAction extends BaseAction {
	
	private static final Logger LOG = Logger.getLogger(FileOpAction.class);
	
	/**文件的上传路径*/
	private String filePath;
	/**阿里云文件服务器上的文件名和后缀*/
	private String fileName;
	/**下载文件名*/
	private String downloadFileName;
	/**下载文件流*/
	private InputStream inputFile;
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getDownloadFileName() {
		return downloadFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}
	
	public InputStream getInputFile() {
		return inputFile;
	}

	public void setInputFile(InputStream inputFile) {
		this.inputFile = inputFile;
	}

	/**
	 * 文本编辑器删除已上传的文件
	 * @return
	 */
	/*public String deleteUploadFiles() {
		//获取项目真实路径
		String realPath = ServletActionContext.getServletContext().getRealPath("/uploadImg") + "/";
		//获取文件全路径
		String fileAllPath = realPath + this.filePath;
		//删除已上传的文件
		FileUploadUtil.deleteFiles(new File(fileAllPath));
		return SUCCESS;
	}*/
	
	public String viewImg() {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request= ServletActionContext.getRequest();
			InputStream in = null;
			BufferedOutputStream bos = null;
			request.setCharacterEncoding("UTF-8");
			FUtil fUtil= new FUtil(); 
			System.out.println(fileName.lastIndexOf("."));
			in=fUtil.downloadFile(fileName.substring(0,fileName.lastIndexOf(".")));
			String suffix=fileName.substring(fileName.lastIndexOf(".")+1);
		    String contentType=EnumGetFileContentType.valueOf(suffix).toString();
		    response.setContentType(contentType);
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buff = new byte[2048];
			int bytesRead;
			if(in != null) {
				while (-1 != (bytesRead = in.read(buff, 0, buff.length))) {
					bos.write(buff, 0, bytesRead);
				}
				in.close();
			}
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	
	/**
	 * 下载文件
	 * @return
	 */
	public String downloadFile(){
		try {
			FUtil fUtil = new FUtil(); 
		    this.downloadFileName = ExcelHelper.getFileNameByUserAgent(this.fileName);
		    this.inputFile = fUtil.downloadFile(this.fileName.substring(0, this.fileName.lastIndexOf(".")));
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return "down";
	}
	
	/**
	 * 删除文件
	 * @return
	 */
	public String deleteFile() {
		FUtil fUtil= new FUtil(); 
		fUtil.deleteFile(this.fileName);
		return SUCCESS;
	}
}
